package com.zoe.tree.controller;

import com.zoe.tree.entity.SysDept;
import com.zoe.tree.service.SysDeptService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * @author zbg
 * @date 2023/06/20
 * @description
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("sys-dept")
public class SysDeptController {

    private final SysDeptService sysDeptService;

    /**
     * 通过ID查询
     * @param id ID
     * @return SysDept
     */
    @GetMapping("/{id:\\d+}")
    public SysDept getById(@PathVariable Long id) {
        return sysDeptService.getById(id);
    }

    /**
     * 返回树形菜单集合
     * @return 树形菜单
     */
    @GetMapping(value = "/tree")
    public Set<SysDept> listDeptTrees() {
        return sysDeptService.listDeptTrees();
    }

    /**
     * 添加
     * @param sysDept 实体
     * @return success/false
     */
    @PostMapping
    public boolean save(@RequestBody SysDept sysDept) {
        return sysDeptService.saveDept(sysDept);
    }

    /**
     * 删除
     * @param id ID
     * @return success/false
     */
    @DeleteMapping("/{id:\\d+}")
    public boolean removeById(@PathVariable Long id) {
        return sysDeptService.removeDeptById(id);
    }

    /**
     * 编辑
     * @param sysDept 实体
     * @return success/false
     */
    @PutMapping
    public boolean update(@RequestBody SysDept sysDept) {
        return sysDeptService.updateDeptById(sysDept);
    }

    // {
    //   "deptId": 1665990533707276290,
    //   "parentId": 1,
    //   "name": "运营小组111",
    //   "nextId": 6
    // }

    /**
     * 编辑 前端不要传sort值
     * @param sysDept 实体
     * @return success/false
     */
    @PutMapping("move")
    public boolean move(@RequestBody SysDept sysDept) {
        return sysDeptService.move(sysDept);
    }


    /**
     * 查收子级id列表
     * @return 返回子级id列表
     */
    @GetMapping(value = "/child-id/{deptId:\\d+}")
    public List<Long> listChildDeptId(@PathVariable Long deptId) {
        return sysDeptService.listChildDeptId(deptId);
    }

}
